package org.ontoexplorer.ui.dialog;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class OntologyChooseDialog extends JDialog{
	
	public static final int CANCELED =2;
	public static final int OK =1;
	public int selectedOption = 0;
	
	private JTextField ontology;
	
	public OntologyChooseDialog() {
	}

	public OntologyChooseDialog(Frame owner, String title, Boolean modal) {
		super(owner, title, modal);
				
		setContentPane(getPane());
		setMinimumSize(new Dimension( 400,200));
		setLocationRelativeTo(owner);
	}	
	
	public JPanel getPane()
	{
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		
		panel.add(createMiddlePanel(), BorderLayout.CENTER);
		panel.add(createSouthPanel(), BorderLayout.SOUTH);
				
		return panel;		
	}
	
	public JPanel createSouthPanel()
	{
		JPanel southPanel = new JPanel();
		southPanel.setLayout(new GridBagLayout());
		GridBagConstraints constr = new GridBagConstraints();
		
		constr.anchor = GridBagConstraints.EAST;
		constr.weightx = 100;
		constr.ipadx=5;
		constr.ipady=5;	
		constr.insets = new Insets(5,5,5,5);
		
		JButton ok = new JButton("Ok");
		JButton cancel = new JButton("Cancel");
		
		ActionListener cancelListener = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				selectedOption = CANCELED;				
				OntologyChooseDialog.this.setVisible(false);				
			}
		};		
		
		
		ActionListener okListener = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				selectedOption = OK;
				OntologyChooseDialog.this.setVisible(false);				
			}
		}; 
		
		cancel.addActionListener(cancelListener);
		ok.addActionListener(okListener);
		
		southPanel.add(new JPanel(), constr);
		constr.weightx=0;
		southPanel.add(ok, constr);
		southPanel.add(cancel, constr);
		
		return southPanel;		
	}
	
	public JPanel createMiddlePanel()
	{
		JPanel panel = new JPanel();
		GridBagConstraints constr = new GridBagConstraints();
		panel.setLayout(new GridBagLayout());
		constr.gridx=0;
		constr.gridy=0;
		constr.gridwidth =1;
		constr.ipadx=5;
		constr.ipady=5;		
		
		constr.insets = new Insets(5,5,5,5);
		
		JLabel label = new JLabel();
		label.setText("Path (http://):");
		panel.add(label, constr);
		 ontology = new JTextField();
		constr.fill = GridBagConstraints.HORIZONTAL;
		constr.weightx = 100;
		constr.gridwidth=2;
		constr.gridx++;
		panel.add(ontology, constr);
		
		ontology.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String text = ontology.getText();
				
				System.out.println(text);
				
			}
		});
		
		return panel;
	}

	public String getURI() {
		return ontology.getText();
	}
	
}
